package com.rzgeo.wcgallay.comp;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.CompoundButton;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Switch;
import android.widget.TextView;

import com.rzgeo.wcgallay.R;
import com.rzgeo.wcgallay.model.ElvInfo;
import com.rzgeo.wcgallay.model.WcmSysFolder;
import com.rzgeo.wcgallay.util.WcmUtil;

import java.io.File;
import java.util.ArrayList;

/**
 * Created by zhanghang on 2017/12/10.
 */

public class WcmReseGallary extends RelativeLayout {
//    private Layer _demLayer;

    private ListView _reseList;
    private ReseAdapter _adapte;
    private AdapterView.OnItemClickListener _onItemSelect=new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
            if(_onSelect!=null)
                _onSelect.onViewAtRese(_adapte._parts[position],position,_adapte._parts[position].iCurrentInfo);
        }
    };
    private OnSelect _onSelect;
    private WcmValueTxv _txvSum;
    private TextView _txvRange;
    private WcmValueTxv _txvCurrentSum;
    private View setColorPanel;

    public WcmReseGallary(Context context) {
        super(context);
    }

    public WcmReseGallary(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public WcmReseGallary(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();

        _reseList=findViewById(R.id.lv_reses);

        _txvSum=findViewById(R.id.txv_sum);
        _txvRange=findViewById(R.id.txv_rang);
        _txvCurrentSum=findViewById(R.id.txv_current_sum);
        this.setColorPanel =  findViewById(R.id.img_set_color_panle);

        _adapte=new ReseAdapter(getContext());
        _reseList.setAdapter(_adapte);

        _reseList.setOnItemClickListener(_onItemSelect);


    }

    public void setup(OnSelect onSelect){
        _onSelect=onSelect;
    }

    public void setData(WcmSysFolder.WcmReseFolder.WcmPart[] parts, int select) {
        _adapte.setData(parts);
        _reseList.setSelection(select);
//        _demLayer=demLayer;
    }

    public void setInfo(float volumeSum, float minHeight, float maxHeight) {
        _txvSum.setValue(volumeSum,WcmUtil.Const.VolumeUtils,1,false);
        _txvRange.setText(Integer.toString((int)minHeight)+"~"+Integer.toString((int)maxHeight));
    }

    public void setPartValue(int iPart, ArrayList<ElvInfo> elv, int  iElv) {
        WcmSysFolder.WcmReseFolder.WcmPart part = _adapte.getData()[iPart];
        part.iCurrentInfo=iElv;
        part.currentElv=elv.get(iElv).elv;
        part.currentVolume=elv.get(iElv).volume;

        float sumVolume = 0;
        for(WcmSysFolder.WcmReseFolder.WcmPart p:_adapte.getData()){
            if(p.iCurrentInfo>-1)
                sumVolume+=p.currentVolume;
        }
        _txvCurrentSum.setValue(sumVolume, WcmUtil.Const.VolumeUtils,1,false);
        _adapte.notifyDataSetChanged();
    }


    public interface OnSelect {
        void onViewAtRese(WcmSysFolder.WcmReseFolder.WcmPart rese, int iPart, int currentElv);

        void onSwitchElvRand(int position, boolean isChecked);

        void onChangedDemColors(int iPart,File tpk);
    }

    private class ReseAdapter extends BaseAdapter {
        private final Context _context;
        private final LayoutInflater _inflater;
        private WcmSysFolder.WcmReseFolder.WcmPart[] _parts =new WcmSysFolder.WcmReseFolder.WcmPart[0];

        public ReseAdapter(Context context) {
            super();
            _context=context;
            _inflater=LayoutInflater.from(_context);
        }

        @Override
        public int getCount() {
            return _parts.length;
        }

        @Override
        public Object getItem(int i) {
            return _parts[i];
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View convertView, ViewGroup viewGroup) {

            ReseViewHolder holder;
            if (convertView == null) {
                convertView = _inflater.inflate(R.layout.item_rese_list, null);
                holder = new ReseViewHolder(convertView);
                convertView.setTag(holder);
            } else {
                holder = (ReseViewHolder) convertView.getTag();
            }


            holder.setData(_parts[i],i);
            return convertView;
        }

        public void setData(WcmSysFolder.WcmReseFolder.WcmPart[] parts) {
            _parts =parts;
            notifyDataSetChanged();
        }

        public WcmSysFolder.WcmReseFolder.WcmPart[] getData() {
            return _parts;
        }

        public class ReseViewHolder implements OnClickListener, CompoundButton.OnCheckedChangeListener {

            private OnShowSetColorClick _onSetColorPanelClick;

            private TextView txvElv;
            public TextView txvTitle;
            public WcmValueTxv txvVolume;
            public ImageView imgSnap;
            public Switch swShowElvRand;
            public ImageButton imgLocate;
            public View btnSetColor;

//            private WcmSysFolder.WcmReseFolder.WcmPart data;
            private int position;

            public ReseViewHolder(View convertView) {

                this.txvTitle = convertView.findViewById(R.id.txv_title);
                this.txvElv = convertView.findViewById(R.id.txv_elv);
                this.txvVolume = convertView.findViewById(R.id.txv_volume);
                this.swShowElvRand = convertView.findViewById(R.id.sw_show_elv_rand);

                this.imgSnap =  convertView.findViewById(R.id.img_snap);
                this.imgLocate =  convertView.findViewById(R.id.img_locate);

                this.btnSetColor =  convertView.findViewById(R.id.img_set_color);

                _onSetColorPanelClick=new OnShowSetColorClick(setColorPanel,_context);

                this.imgSnap.setOnClickListener(this);
                this.txvTitle.setOnClickListener(this);
                this.imgLocate.setOnClickListener(this);
                this.swShowElvRand.setOnCheckedChangeListener(this);

                this.btnSetColor.setOnClickListener(_onSetColorPanelClick);
            }

            @Override
            public void onClick(View v) {
                _onItemSelect.onItemClick(null,null,this.position,0);
            }

            public void setData(WcmSysFolder.WcmReseFolder.WcmPart data,int posistion) {
//                this.data = data;
                this.position=posistion;

                this.txvTitle.setText(data.getName());

                this.txvElv.setText(Float.toString(data.currentElv));
                this.txvVolume.setValue(data.currentVolume, WcmUtil.Const.VolumeUtils,1,false);
                swShowElvRand.setChecked(true);
                _onSetColorPanelClick.setData(data.demPaths,posistion);
//                File snap=new File(data.getSnapPath());
//                if(snap.exists()){
//                    this.imgSnap.setImageDrawable(BitmapDrawable.createFromPath(snap.getAbsolutePath()));
//                }
//                else{
//                    this.imgSnap.setImageDrawable(getContext().getResources().getDrawable(android.R.drawable.stat_sys_upload));
//                }

            }

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                _onSelect.onSwitchElvRand(this.position,isChecked);
            }
        }
    }

    private class OnShowSetColorClick implements OnClickListener, AdapterView.OnItemClickListener {
        private final Context _context;
        private ListView _lvDemColors;
        private ImageView _btnColorBelt;
        private View _settingPanelView;
        private ColorBeltAdapter _colorAdapt;
        private WcmSysFolder.WcmReseFolder.WcmDemWithColors[] _colors;
        private int _iRese;

        public OnShowSetColorClick(View settingPanelView, Context context) {
            this._settingPanelView = settingPanelView;
            this._context=context;
        }

        @Override
        public void onClick(View v) {
            this._settingPanelView.setVisibility(VISIBLE);

            this._btnColorBelt =  this._settingPanelView.findViewById(R.id.btn_close_set_panel);
            this._lvDemColors =  this._settingPanelView.findViewById(R.id.lv_dem_colors);

            this._btnColorBelt.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    _settingPanelView.setVisibility(GONE);
                }
            });
            _colorAdapt=new ColorBeltAdapter(_context);
            _colorAdapt.setData(_colors);
            _lvDemColors.setAdapter(_colorAdapt);
            _colorAdapt.setData(_colors);
            _lvDemColors.setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
            _lvDemColors.setOnItemClickListener(this);
        }


        public void setData(WcmSysFolder.WcmReseFolder.WcmDemWithColors[]colors ,int iRese) {
            _colors=colors;
            this._iRese=iRese;

            if(_colorAdapt!=null)
                _colorAdapt.setData(colors);
        }

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            WcmSysFolder.WcmReseFolder.WcmDemWithColors color =
                    (WcmSysFolder.WcmReseFolder.WcmDemWithColors) _colorAdapt.getItem(position);

            if(_onSelect!=null)
                _onSelect.onChangedDemColors(_iRese,color.DemTpk);
//            index=_layer.;
        }

        private class ColorBeltAdapter extends BaseAdapter {
            private final Context _context;
            private final LayoutInflater _inflater;
            private WcmSysFolder.WcmReseFolder.WcmDemWithColors[] _colors;

            public ColorBeltAdapter(Context context) {
                super();
                _context = context;
                _inflater = LayoutInflater.from(_context);
                _colors=new WcmSysFolder.WcmReseFolder.WcmDemWithColors[0];
            }

            @Override
            public int getCount() {
                if(_colors==null)
                    return 0;
                else
                    return _colors.length;
            }

            @Override
            public Object getItem(int position) {
                return _colors[position];
            }

            @Override
            public long getItemId(int position) {
                return position;
            }

            @Override
            public View getView(int i, View convertView, ViewGroup parent) {

                ColorViewHolder holder;
                if (convertView == null) {
                    convertView = _inflater.inflate(R.layout.item_rese_list_color_list_item, null);
                    holder = new ColorViewHolder(convertView);
                    convertView.setTag(holder);
                } else {
                    holder = (ColorViewHolder) convertView.getTag();
                }


                holder.setData(_colors[i],i);
                return convertView;
            }


            public void setData(WcmSysFolder.WcmReseFolder.WcmDemWithColors[]colors) {
                _colors=colors;
                notifyDataSetChanged();
            }

            public class ColorViewHolder {
                private final ImageView _imgColorBelt;
                private int position;
                private WcmSysFolder.WcmReseFolder.WcmDemWithColors color;

                public ColorViewHolder(View convertView) {
                    this._imgColorBelt=convertView.findViewById(R.id.img_color_belt);
                }

                public void setData(WcmSysFolder.WcmReseFolder.WcmDemWithColors color, int position) {
                    this.position=position;
                    this.color=color;
                    _imgColorBelt.setImageDrawable(BitmapDrawable.createFromPath(color.ColorBeltLegend.getAbsolutePath()));
                }
            }
        }

    }
}
